package br.com.wai2008.client.system;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * This class calls a system command execution on Linux operation
 * system.
 * 
 * @since 
 * 		September, 2008.
 * 
 * @version
 * 		WAI2008 - version 1.0
 * 
 * @author 
 * 		Andre Romero Goncales
 */
public class SystemCommand {

	/**
	 * Calls an execution of any system command on Linux operation
	 * system.
	 * 
	 * @param
	 * 		command Array containing the command.<br>
	 * 		Example: String[0] = sudo, String[1] = ls.
	 * 
	 * @return
	 * 		The system command execution's return in a buffer or <b>null</>
	 * 		if an error occurs.
	 */
	public static BufferedReader executeSystemCommand(String... command) {
		System.out.println("[SystemCommand] Executing system command...");
		if (System.getProperty("os.name").equals("Linux")) {
			Process Findspace;
			try {
				Findspace = Runtime.getRuntime().exec(command);
				System.out.println("[SystemCommand] System command executed");
		        return new BufferedReader(new InputStreamReader (Findspace.getInputStream()));
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
		System.out.println("[SystemCommand] ERROR: Cannot execute system command");
		return null;
	}
}
